package com.atguigu.guli.service.ucenter.service.impl;

import com.atguigu.guli.common.utils.FormUtils;
import com.atguigu.guli.common.utils.MD5;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.MemberRegisForm;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-11
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // 注册用户
    @Override
    public void saveMember(MemberRegisForm memberRegis) {
        // 1, 判断提交的信息是否完整
        if (StringUtils.isEmpty(memberRegis.getNickname()) ||
                StringUtils.isEmpty(memberRegis.getMobile()) ||
                StringUtils.isEmpty(memberRegis.getPassword()) ||
                StringUtils.isEmpty(memberRegis.getCode()) ||
                !FormUtils.isMobile(memberRegis.getMobile())
        ) {
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }

        // 2,判断手机号是否已经注册
        String mobile = memberRegis.getMobile();
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Integer memberCount = memberMapper.selectCount(queryWrapper);
        if (memberCount > 0) {
            throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }

        // 3, 判断验证码是否正确
        Object mobileObj = redisTemplate.opsForValue().get("code:code:" + mobile);
        if (!(memberRegis.getCode() != null && memberRegis.getCode().equals(mobileObj.toString()))) {
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }


        // 4, 注册用户
        Member member = new Member();
        BeanUtils.copyProperties(memberRegis, member);
        // 设置用户的默认头像
        member.setAvatar("https://sh-gulionline-imgs.oss-cn-shanghai.aliyuncs.com/avatar/2021/06-29/default.jpg");
        // 密码使用MD5加密后保存到数据库(暂时), 开发中使用SpringSecurity加密
        member.setPassword(MD5.encrypt(member.getPassword()));

        memberMapper.insert(member);

        // 注册成功, 清除redis中保存的验证码
        redisTemplate.delete("code:code:" + mobile);

    }

    // 用户登录
    @Override
    public String login(String mobile, String password) {

        // 1,判断手机号和密码都不能为空
        if (mobile == null || password == null) {
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_PASSWORD_ERROR);
        }

        // 2,查询手机号是否存在, 账号密码是否正确
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = memberMapper.selectOne(queryWrapper);
        if (member == null) {
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }

        // 因为密码使用MD5加密, 需要将用户输入的密码加密后比较
        if (!member.getPassword().equals(MD5.encrypt(password))) {
            throw new GuliException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //构建jwt
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setAvatar(member.getAvatar());
        jwtInfo.setNickname(member.getNickname());
        String token = JwtHelper.createToken(jwtInfo);
        return token;

    }
}
